package cn.scy.business.service.xiamdx;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.scy.base.PageResult;
import cn.scy.business.domain.entity.xiamdx.XiamdxReportInfo;
import cn.scy.business.domain.param.xiamdx.XiamdxReportInfoParam;
import cn.scy.business.domain.vo.xiamdx.XiamdxReportInfoVO;
import cn.scy.business.mapper.xiamdx.XiamdxReportInfoMapper;
import cn.scy.business.service.NumMemberInfoService;
import cn.scy.constant.BusinessConst;
import cn.scy.exception.BusinessException;
import cn.scy.util.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 报备管理-报备信息表 服务实现类
 *
 * @author scy
 * @since 2025-04-26
 */
@Service
public class XiamdxReportInfoServiceImpl extends ServiceImpl<XiamdxReportInfoMapper, XiamdxReportInfo> implements XiamdxReportInfoService {

    @Resource
    private NumMemberInfoService memberInfoService;

    @Resource
    private XiamdxNumberInfoService numberInfoService;

    @Override
    public PageResult<XiamdxReportInfoVO> listPage(XiamdxReportInfoParam param) {
        // 构建分页对象
        Page<XiamdxReportInfoVO> pageQuery = PageUtil.convert2PageQuery(param);
        Page<XiamdxReportInfoVO> pageData = baseMapper.listPage(pageQuery, param);
        // 对查询结果进行脱敏处理
        List<XiamdxReportInfoVO> desensitizedRecords = pageData.getRecords().stream()
                .map(record -> {
                    // 复制原始对象，避免修改数据库记录
                    XiamdxReportInfoVO desensitizedRecord = new XiamdxReportInfoVO();
                    BeanUtils.copyProperties(record, desensitizedRecord);
                    // 对身份证号进行脱敏处理
                    desensitizedRecord.setIdentityCard(DesensitizedUtil.idCardNum(record.getIdentityCard(),4,4));
                    return desensitizedRecord;
                })
                .collect(Collectors.toList());
        return PageUtil.convert2PageResult(pageData, desensitizedRecords);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteReport(String id) {
        // 校验
        XiamdxReportInfo report = this.getById(id);
        if (ObjectUtil.isNull(report)) {
            throw new BusinessException("报备信息不存在");
        }
        // 删除报备信息
        return this.removeById(id);
    }

}
